/*
 * ControllerServlet.java
 *
 * Created on 9 August 2003, 10:58
 */
package com.enterprise.web;

import com.assg2.dao.DaoShow;
import com.movie.Show;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Controller Servlet that accepts all client requests and performs the
 * lookup required to process the request. This class uses the command
 * design pattern to find the required <i>Command</i> class that will
 * process the request.
 * 
 * @author $author
 * @version
 */
public class ControllerServlet extends HttpServlet {

    private Map commands;

    /**
     * Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {

        super.init(config);

        commands = new HashMap();

        commands.put("add", new AddCommand());
        commands.put("delete", new DeleteCommand());
        commands.put("list", new ListCommand());
        commands.put("message", new MessageCommand());
        commands.put("login", new LoginCommand());
        commands.put("logout", new LogoutCommand());
        commands.put("viewerpage", new ViewerPage());
        commands.put("ownerpage", new ErrorCommand());
        commands.put("movieinfo", new InfoCommand());
        commands.put("review", new ReviewCommand());
        commands.put("book", new BookCommand());
        commands.put("register", new RegisterCommand());
        commands.put("search", new SearchCommand());
        commands.put("addshow", new AddShowCommand());
        commands.put("addshowtime", new AddShowTimeCommand());
        commands.put("confirmbooking", new ConfirmBookingCommand());
        commands.put("finishconfirm", new FinishConfirmCommand());
        commands.put("editprofile", new EditProfileCommand());
        commands.put("editviewerprofile", new EditViewerProfileCommand());
        commands.put("searchbygenre", new SearchByGenreCommand());
        commands.put("searchfriend", new SearchFriendCommand());
        commands.put("recommend", new RecommendCommand());
        commands.put("friendinfo", new FriendInfoCommand());
        commands.put("friendadd", new FriendAddCommand());
        commands.put("friendrequest", new FriendRequestCommand());
        commands.put("requestlist", new RequestListCommand());
        commands.put("acceptrequest", new AcceptRequestCommand());
        commands.put("rejectrequest", new RejectRequestCommand());
        commands.put("friendlist", new FriendListCommand());
        commands.put("switchemailpublic", new SwitchMailPublicCommand());
        commands.put("switchbirthdaypublic", new SwitchBirthdayPublicCommand());
        commands.put("friendedinfo", new FriendedInfoCommand());
        commands.put("PAGE_NOT_FOUND", new ErrorCommand());


    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Command cmd = resolveCommand(request);
        String next = cmd.execute(request, response);
        if (next.indexOf('.') < 0) {
            cmd = (Command) commands.get(next);
            next = cmd.execute(request, response);
        }
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(next);
        dispatcher.forward(request, response);
    }

    private Command resolveCommand(HttpServletRequest request) {
        Command cmd = (Command) commands.get(request.getParameter("operation"));
        if (cmd == null) {
            cmd = (Command) commands.get("PAGE_NOT_FOUND");
        }
        return cmd;
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String showid = request.getParameter("show");

        Map<String, String> messages = new HashMap<String, String>();

        if (showid != null) {

            DaoShow daoshow = new DaoShow();
            Show show = (Show) daoshow.findById(Integer.parseInt(showid));

            String number = request.getParameter("numseatshow" + show.getIdshow());
            // Email is specified as request parameter, do the business logic here.
            if (number == null) {
                messages.put("error", "Please enter a value");
            } else if (Integer.getInteger(number) != null) {
                messages.put("error", "Enter a valid number");
            } else {
                // Do something with email here?
                messages.put("success", "Thank you!");
            }
        }

        request.setAttribute("messages", messages);

        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Controller for the application";
    }
}
